//
//  YXJTableView.m
//  Piece
//
//  Created by dllo on 16/6/7.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "YXJTableView.h"
#import "NSString+YXJHeight.h"

@implementation YXJTableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        //初始化tableView
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 113) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.separatorStyle = 0;
        [self addSubview:_tableView];
        

        
        [self.tableView registerClass:[YXJMusicStoryCell class] forCellReuseIdentifier:@"musicStroyCell"];
        [self.tableView registerClass:[YXJListCell class] forCellReuseIdentifier:@"listCell"];
        [self.tableView registerClass:[YXJCommentCell class] forCellReuseIdentifier:@"commentCell"];
        [self.tableView registerClass:[YXJMusicCell class] forCellReuseIdentifier:@"musicCell"];
        [self.tableView registerClass:[YXJLikeMusicCell class] forCellReuseIdentifier:@"likeMusicCell"];
        
        //初始化headView
        self.headView = [[UIView alloc] initWithFrame:CGRectMake(-30, 0, ScreenWidth, ScreenHeight - 143 - 35)];
        self.tableView.tableHeaderView = _headView;
        
        //初始化歌曲背景图片
        self.photoImageView = [[UIImageView alloc] init];
        [self.headView addSubview:_photoImageView];
        
        //初始化歌曲信息view
        self.contentView = [[YXJView alloc] init];
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.contentView.layer.borderWidth = 2;
        self.contentView.layer.borderColor = [UIColor colorWithRed:0.87 green:0.87 blue:0.88 alpha:1.00].CGColor;
        [self.headView addSubview:_contentView];
        
        __weak YXJTableView *tV = self;
        _contentView.clickBlock = ^()
        {
            tV.clickBlock();
        };
        
        //初始化音乐故事
        self.musicStoryLabel = [[UILabel alloc] init];
        self.musicStoryLabel.text = @"背景故事";
        [self.musicStoryLabel setTextColor:[UIColor colorWithRed:0.73 green:0.73 blue:0.74 alpha:1.00]];
        self.musicStoryLabel.font = [UIFont systemFontOfSize:13];
        [self.headView addSubview:_musicStoryLabel];
        
        //初始化音乐故事按钮
        self.storyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_storyButton addTarget:self action:@selector(didStoryButton:) forControlEvents:UIControlEventTouchUpInside];
        [_storyButton setImage:[UIImage imageNamed:@"music_story_selected"] forState:UIControlStateNormal];
        _storyButton.tag = 1001;
        self.whichPage = 1;
        [self.headView addSubview:_storyButton];
        
        //初始化歌词按钮
        self.lyricButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_lyricButton addTarget:self action:@selector(didLyricButton:) forControlEvents:UIControlEventTouchUpInside];
        [_lyricButton setImage:[UIImage imageNamed:@"music_lyric_normal"] forState:UIControlStateNormal];
        _lyricButton.tag = 1002;
        [self.headView addSubview:_lyricButton];
        
        //初始化歌曲信息按钮
        self.aboutButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_aboutButton addTarget:self action:@selector(didAboutButton:) forControlEvents:UIControlEventTouchUpInside];
        [_aboutButton setImage:[UIImage imageNamed:@"music_about_normal"] forState:UIControlStateNormal];
        _aboutButton.tag = 1003;
        [self.headView addSubview:_aboutButton];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.photoImageView.frame = CGRectMake(0, 0, ScreenWidth, ScreenWidth);
    self.contentView.frame = CGRectMake(10, ScreenWidth - 15, ScreenWidth - 20, 110);
    self.musicStoryLabel.frame = CGRectMake(_contentView.frame.origin.x, ScreenWidth + _contentView.frame.size.height, 100, 20);
    self.storyButton.frame = CGRectMake(ScreenWidth - 160, _musicStoryLabel.frame.origin.y - 10, 44, 44);
    self.lyricButton.frame = CGRectMake(ScreenWidth - 105, _storyButton.frame.origin.y, 44, 44);
    self.aboutButton.frame = CGRectMake(ScreenWidth - 50, _storyButton.frame.origin.y, 44, 44);
    [self.tableView setContentOffset:CGPointMake(0, 180)];
    
}

#pragma mark -- button的点击方法
- (void)didStoryButton:(UIButton *)sender
{
    self.musicStoryLabel.text = @"背景故事";
    self.whichPage = 1;
    [sender setImage:[UIImage imageNamed:@"music_story_selected"] forState:UIControlStateNormal];
    [_lyricButton setImage:[UIImage imageNamed:@"music_lyric_normal"] forState:UIControlStateNormal];
    [_aboutButton setImage:[UIImage imageNamed:@"music_about_normal"] forState:UIControlStateNormal];
    [self.tableView reloadData];
}

- (void)didLyricButton:(UIButton *)sender
{
    self.musicStoryLabel.text = @"歌词";
    [sender setImage:[UIImage imageNamed:@"music_lyric_selected"] forState:UIControlStateNormal];
    [_storyButton setImage:[UIImage imageNamed:@"music_story_normal"] forState:UIControlStateNormal];
    [_aboutButton setImage:[UIImage imageNamed:@"music_about_normal"] forState:UIControlStateNormal];
    self.whichPage = 2;
    [self.tableView reloadData];
}

- (void)didAboutButton:(UIButton *)sender
{
    self.musicStoryLabel.text = @"相关信息";
    [sender setImage:[UIImage imageNamed:@"music_about_selected"] forState:UIControlStateNormal];
    [_storyButton setImage:[UIImage imageNamed:@"music_story_normal"] forState:UIControlStateNormal];
    [_lyricButton setImage:[UIImage imageNamed:@"music_lyric_normal"] forState:UIControlStateNormal];
    self.whichPage = 3;
    [self.tableView reloadData];
}


#pragma mark -- tableView的协议方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    if (section == 1) {
        return self.contentDataSource.count;
        
    }
    if (section == 2) {
        return self.likeMusicDataSource.count;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (self.whichPage == 1) {
            NSString *str = self.model.story;
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
            CGRect newRect = [str boundingRectWithSize:CGSizeMake(ScreenWidth - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
            return newRect.size.height + 130;
        }
        if (self.whichPage == 2) {
            NSString *str = self.model.lyric;
            NSArray *lyricArray = [str componentsSeparatedByString:@"／"];
            NSString *lyricString = [lyricArray componentsJoinedByString:@"\n"];
            
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
            CGRect newRect = [lyricString boundingRectWithSize:CGSizeMake(ScreenWidth - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
            return newRect.size.height + 60;
        }
        if (self.whichPage == 3) {
            NSString *str = self.model.info;
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
            CGRect newRect = [str boundingRectWithSize:CGSizeMake(ScreenWidth - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
            return newRect.size.height + 60;
        }
        
    }
    
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            return 40;
        }  else  {
            if (self.contentDataSource.count != 0) {
                NSString *str = [[self.contentDataSource objectAtIndex:indexPath.row] content];
                NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16], NSFontAttributeName, nil];
                CGRect newRect = [str boundingRectWithSize:CGSizeMake(ScreenWidth - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
                return newRect.size.height + 80;
            }
        }
    }
    if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            return 40;
        }
        else{
            return 60;
        }
    }
    return 0;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
                YXJMusicStoryCell *cell = [[YXJMusicStoryCell alloc] init];
                cell.page = self.whichPage;
                cell.model = self.model;
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                return cell;
    }

    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            YXJListCell *cell = [[YXJListCell alloc] init];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
        if (indexPath.row != 0) {
            YXJCommentCell *cell = [[YXJCommentCell alloc] init];
            
            cell.contentModel = [self.contentDataSource objectAtIndex:indexPath.row];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    }
    
    if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            YXJLikeMusicCell *cell = [[YXJLikeMusicCell alloc] init];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
        else{
            YXJMusicCell *cell = [[YXJMusicCell alloc] init];
            cell.musicModel = [self.likeMusicDataSource objectAtIndex:indexPath.row];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            
        }
        else{
            
            NSInteger pageID = [[self.likeMusicDataSource objectAtIndex:indexPath.row] id];
             _myBlock(indexPath.row, pageID);
            
        }
    }
}

#pragma mark -- 重写各种model和数组
-(void)setModel:(YXJMusicModel *)model
{
    if (_model != model) {
        _model = model;
        [self.tableView reloadData];
    }
}

-(void)setContentModel:(YXJContentModel *)contentModel
{
    if (_contentModel != contentModel) {
        _contentModel = contentModel;
        [self.tableView reloadData];
    }
}

-(void)setLikeMusicModel:(YXJLikeMusicModel *)likeMusicModel
{
    if (_likeMusicModel != likeMusicModel) {
        _likeMusicModel = likeMusicModel;
        [self.tableView reloadData];
    }
}

- (void)setPageNumber:(NSInteger)pageNumber
{
    if (_pageNumber != pageNumber) {
        _pageNumber = pageNumber;
        self.contentView.pageNumber = pageNumber;
        [self.tableView reloadData];
    }
}

#pragma mark -------------------- section滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.tableView.contentOffset.y < - 120) {
        [self.delegate sweipWay];
    }
    
    CGFloat y = self.tableView.contentOffset.y / 10 - 18;
    
    if (y >= 20)
    {
        self.contentView.frame = CGRectMake(10 , ScreenWidth - 15 + 20, ScreenWidth - 20, 110 );
    }
    else if (y <= -10)
    {
        self.contentView.frame = CGRectMake(10, ScreenWidth - 15 - 10, ScreenWidth - 20, 110);
    }
    else
    {
        self.contentView.frame = CGRectMake(10, ScreenWidth - 15 + y, ScreenWidth - 20, 110 );
    }
}



@end
